﻿using System;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.XPath;

namespace InfoJetSoft.Service.Worker
{

    class XSNViewFormatControlWorker
    {
        //CID 0945 //2011.06.13 //BindingTest
        public static void formatControl(View view, BindingTest bindingTest)
        {
            //CID 0945 //2011.06.13 //Begin
            if (view.Manifest.EnableBindingTest)
            {
                bindingTest.TestView(view);
            }
            //CID 0945 //2011.06.13 //End

            //CID 1422 //2012.10.29 //Begin
            List<XmlElement> inlinePictureControlList = null;
            //CID 1422 //2012.10.29 //End
            //CID 1423 //2012.10.31 //Begin
            List<XmlElement> fileAttachmentControlList = null;
            //CID 1423 //2012.10.31 //End
            //CID 1429 //2012.11.02 //Begin
            List<XmlElement> linkPictureControlList = null;
            //CID 1429 //2012.11.02 //End


            XmlElement root = view.Doc.DocumentElement;
            string xdBinding = NamespaceUtil.toFullName(view.XdPrefix, JetConstant.XD_binding);
            string xdXctname = NamespaceUtil.toFullName(view.XdPrefix, JetConstant.XD_xctname);
            string controlFilter = "//*[@" + xdBinding + "]";
            XmlNodeList elementList = DOMUtil.selectNodes(root, controlFilter, 0, view.NsResolver);
            foreach (XmlElement control in elementList)
            {
                string xctname = control.GetAttribute(xdXctname);
                if (xctname.Length > 0)
                {
                    if (xctname.Equals(JetConstant.XCT_ExpressionBox))
                    {
                        continue;
                    }
                    if (xctname.Equals(JetConstant.XCT_Section) || xctname.Equals(JetConstant.XCT_RepeatingSection) || xctname.Equals(JetConstant.XCT_choicegroup))
                    {
                        assignControlID(view, control, xdBinding, xctname, false);
                    }
                    else if (xctname.Equals(JetConstant.XCT_LinkedImage))
                    {
                        //CID 1429 //2012.11.02 //Begin
                        if (linkPictureControlList == null)
                        {
                            linkPictureControlList = new List<XmlElement>();
                        }
                        linkPictureControlList.Add(control);
                        //CID 1429 //2012.11.02 //End
                    }
                    else if (xctname.Equals(JetConstant.XCT_InlineImage))
                    {
                        //CID 1422 //2012.10.29 //Begin
                        if (inlinePictureControlList == null)
                        {
                            inlinePictureControlList = new List<XmlElement>();
                        }
                        inlinePictureControlList.Add(control);
                        //CID 1422 //2012.10.19 //Begin
                    }
                    else if (xctname.Equals(JetConstant.XCT_FileAttachment))
                    {
                        //CID 1423 //2012.10.31 //Begin
                        if (fileAttachmentControlList == null)
                        {
                            fileAttachmentControlList = new List<XmlElement>();
                        }
                        fileAttachmentControlList.Add(control);
                        //CID 1423 //2012.10.31 //End
                    }
                    else if (xctname.ToLower().Equals(JetConstant.XCT_inkpicture))
                    {
                        //CID 0499 //2010.03.17 //Begin
                        addInkPictureEvent(control);
                        assignControlID(view, control, xdBinding, xctname, false);
                        //CID 0499 //2010.03.17 //End
                        //CID 0861 //2011.04.12 //Begin
                        string binding = control.GetAttribute(xdBinding);
                        if (ConfigUtil.EnableControlPSVI())
                        {
                            control.SetAttribute("psvi_id", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetPSVI(") + binding + ")}");
                        }
                        //CID 0861 //2011.04.12 //End
                    }
                    else
                    {
                        addInputEvent(control);
                        assignControlID(view, control, xdBinding, xctname, true);
                        if (ConfigUtil.EnabledServerControl())
                        {
                            //CID 0945 //2011.06.14 //BindingTest
                            setServerControlAttr(view, control, xdBinding, bindingTest);
                            setSchemaTypeAttr(view, control, xdBinding);
                            checkDatafmt(view, control);
                            //CID 0048 //2008.07.17 //Begin
                            if (ConfigUtil.EnableReplaceGroupingSymbol())
                            {
                                setDoubleKeyUpEvent(control);
                            }
                            //CID 0048 //2008.07.17 //End
                        }
                    }
                }
                else
                {
                    assignControlID(view, control, xdBinding, xctname, true);
                }
            }
            //CID 1422 //2012.10.29 //Begin
            formatInlinePictures(view, inlinePictureControlList, xdBinding);
            //CID 1422 //2012.10.29 //End
            //CID 1423 //2012.10.31 //Begin
            formatFileAttachments(view, fileAttachmentControlList, xdBinding);
            //CID 1423 //2012.10.31 //End
            //CID 1429 //2012.11.02 //Begin
            formatLinkPictures(view, linkPictureControlList, xdBinding);
            //CID 1429 //2012.11.02 //End
        }

        //CID 0048 //2008.07.17 //Begin
        private static void setDoubleKeyUpEvent(XmlElement control)
        {
            string schemaType = control.GetAttribute(JetConstant.HTML_SCHEMA_TYPE);
            if (schemaType != null && schemaType.Equals(SchemaType._double))
            {
                DOMUtil.setEventAttribute(control, "onkeyup", "InfoJet_OnKeyUp(event,this,'control');");
            }
        }
        //CID 0048 //2008.07.17 //End

        private static void checkDatafmt(View view, XmlElement control)
        {
            string datafmt = control.GetAttribute(NamespaceUtil.toFullName(view.XdPrefix, "datafmt"));
            string schemaType = control.GetAttribute(JetConstant.HTML_SCHEMA_TYPE);
            if (datafmt.Length <= 0 && schemaType.Length > 0)
            {
                if (schemaType.Equals(SchemaType._integer))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"number\",\"numDigits:0;grouping:0;negativeOrder:1;\"");
                }
                else if (schemaType.Equals(SchemaType._double))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"number\",\"numDigits:auto;grouping:0;negativeOrder:1;\"");
                }
                else if (schemaType.Equals(SchemaType._boolean))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"boolean\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._string))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"string\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._anyURI))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"anyURI\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._any))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"any\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._date))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"date\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._time))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"time\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._dateTime))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"dateTime\",\"\"");
                }
                else if (schemaType.Equals(SchemaType._unknown))
                {
                    control.SetAttribute("datafmt", JetConstant.URI_XD, "\"unknown\",\"\"");
                }
            }
        }

        //CID 0945 //2011.06.14 //BindingTest
        private static void setServerControlAttr(View view, XmlElement control, string xdBinding, BindingTest bindingTest)
        {
            string binding = control.GetAttribute(xdBinding);
            string bindingNodeName = ExpressionUtil.parseTargetNodeName(binding);
            if (bindingNodeName.Length > 0)
            {
                //CID 0945 //2011.06.14 //Begin
                if (view.Manifest.EnableBindingTest)
                {
                    bindingTest.SetViewControl(view, control);
                }
                //CID 0945 //2011.06.14 //End
                //CID 0076 //2008.07.31 //Begin
                bool isServerNode = XSNManifestServerNodeWorker.isServerNode(view, bindingNodeName);
                //CID 0076 //2008.07.31 //End
                if (isServerNode)
                {
                    control.SetAttribute(JetConstant.HTML_SERVER_CONTROL, JetConstant.YES);
                }
                else
                {
                    control.SetAttribute(JetConstant.HTML_SERVER_CONTROL, JetConstant.NO);
                }
            }
        }

        private static string getNsUri(View view, XmlElement control, string binding, string bindingNodeName)
        {
            string nsUri = DOMUtil.getNodeNamespaceUri(view.Doc, bindingNodeName);
            if (binding.StartsWith("@") && binding.IndexOf(":") < 0)
            {
                XmlElement foreachElement = DOMUtil.findAncestorElementByLocalName(control, "for-each");
                if (foreachElement != null)
                {
                    string foreachSelect = foreachElement.GetAttribute("select");
                    string selectTargetNodeName = ExpressionUtil.parseTargetNodeName(foreachSelect);
                    if (selectTargetNodeName != null)
                    {
                        string selectNsPrefix = NamespaceUtil.getPrefix(selectTargetNodeName);
                        nsUri = view.Doc.DocumentElement.GetNamespaceOfPrefix(selectNsPrefix);
                    }
                }
                else
                {
                    XmlElement templateElement = DOMUtil.findAncestorElementByLocalName(control, "template");
                    if (templateElement != null)
                    {
                        string templateMatch = templateElement.GetAttribute("match");
                        string matchTargetNodeName = ExpressionUtil.parseTargetNodeName(templateMatch);
                        if (matchTargetNodeName != null)
                        {
                            string matchNsPrefix = NamespaceUtil.getPrefix(matchTargetNodeName);
                            nsUri = view.Doc.DocumentElement.GetNamespaceOfPrefix(matchNsPrefix);
                        }
                    }
                }
            }
            if (bindingNodeName.StartsWith("@") && bindingNodeName.IndexOf(":") < 0 && binding.StartsWith("dfs:queryFields"))
            {
                nsUri = JetConstant.URI_Q;
            }
            return nsUri;
        }

        //2008.05.11 //reattachementsnextissue // Begin
        private static void checkNonNilllableBASE64(View view, XmlElement control, string xdBinding)
        {
            string binding = control.GetAttribute(xdBinding);
            string bindingNodeName = ExpressionUtil.parseTargetNodeName(binding);
            if (bindingNodeName.Length > 0)
            {
                string nsUri = getNsUri(view, control, binding, bindingNodeName);

                if (nsUri == null)
                {
                    control.SetAttribute(JetConstant.HTML_SCHEMA_TYPE, "unknown");
                }
                else
                {
                    SchemaType schemaType = SchemaTypeWorker.findSchemaType(view.Manifest, bindingNodeName, nsUri);
                    if (schemaType != null)
                    {
                        control.SetAttribute(JetConstant.HTML_SCHEMA_TYPE, schemaType.Type);
                        control.SetAttribute(JetConstant.HTML_SCHEMA_NILLABLE, schemaType.Nillable);

                        if (schemaType.Type.Equals(SchemaType._base64Binary) && schemaType.Nillable.Equals(SchemaType.false_value))
                        {
                            if (view.Manifest.NonNillableBASE64SchemaTypeTable == null)
                            {
                                view.Manifest.NonNillableBASE64SchemaTypeTable = new Hashtable();
                            }
                            if (!view.Manifest.NonNillableBASE64SchemaTypeTable.ContainsKey(bindingNodeName))
                            {
                                view.Manifest.NonNillableBASE64SchemaTypeTable.Add(bindingNodeName, schemaType);
                            }
                        }
                    }
                }
            }
        }
        //2008.05.11 //reattachementsnextissue //End.

        private static void setSchemaTypeAttr(View view, XmlElement control, string xdBinding)
        {
            string binding = control.GetAttribute(xdBinding);
            //CID 0500 //2010.01.19 //Begin
            if (ConfigUtil.EnableControlPSVI())
            {
                control.SetAttribute("psvi_id", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetPSVI(") + binding + ")}");
            }
            //CID 0500 //2010.01.19 //End
            string bindingNodeName = ExpressionUtil.parseTargetNodeName(binding);
            if (bindingNodeName.Length > 0)
            {
                string nsUri = getNsUri(view, control, binding, bindingNodeName);

                if (nsUri == null)
                {
                    control.SetAttribute(JetConstant.HTML_SCHEMA_TYPE, "unknown");
                }
                else
                {
                    SchemaType schemaType = SchemaTypeWorker.findSchemaType(view.Manifest, bindingNodeName, nsUri);
                    control.SetAttribute(JetConstant.HTML_SCHEMA_TYPE, schemaType.Type);
                    control.SetAttribute(JetConstant.HTML_SCHEMA_NILLABLE, schemaType.Nillable);
                    if (schemaType.MinLength != null)
                    {
                        control.SetAttribute(JetConstant.HTML_SCHEMA_MINLENGTH, schemaType.MinLength);
                    }
                    if (schemaType.MaxLength != null)
                    {
                        control.SetAttribute(JetConstant.HTML_SCHEMA_MAXLENGTH, schemaType.MaxLength);
                    }
                    if (schemaType.MinInclusive != null)
                    {
                        control.SetAttribute(JetConstant.HTML_SCHEMA_MININCLUSIVE, schemaType.MinInclusive);
                    }
                    if (schemaType.MaxInclusive != null)
                    {
                        control.SetAttribute(JetConstant.HTML_SCHEMA_MAXINCLUSIVE, schemaType.MaxInclusive);
                    }

                    //记录所有nillable为true的SchemaType.
                    //CID 0451 //2009.11.08 //if
                    if (schemaType.Nillable.Equals(SchemaType.true_value))
                    {
                        if (view.Manifest.NillableSchemaTypeTable == null)
                        {
                            view.Manifest.NillableSchemaTypeTable = new Hashtable();
                        }
                        if (!view.Manifest.NillableSchemaTypeTable.ContainsKey(bindingNodeName))
                        {
                            view.Manifest.NillableSchemaTypeTable.Add(bindingNodeName, schemaType);
                        }
                    }
                }
            }
        }

        private static void addInputEvent(XmlElement control)
        {
            DOMUtil.setEventAttribute(control, "onkeydown", "InfoJet_OnKeyDown(event,this);");
            DOMUtil.setEventAttribute(control, "onkeypress", "InfoJet_OnKeyPress(event,this);");
            DOMUtil.setEventAttribute(control, "onfocus", "InfoJet_OnFocus(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onmouseover", "InfoJet_OnMouseOver(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onblur", "InfoJet_OnBlur(event,this,'control');");

            if (isButtonTypeInput(control))
            {
                //CID 0131 //2008.10.03 //Begin
                //DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnClick(event,this,'control');InfoJet_OnControlChange(event,this);");
                DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnClick(event,this,'control');");
                //CID 0131 //2008.10.03 //End
            }
            else
            {
                DOMUtil.setEventAttribute(control, "onchange", "InfoJet_OnControlChange(event,this);");
                DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnClick(event,this,'control');");
            }
        }

        private static void addLinkedImageEvent(XmlElement control)
        {
            DOMUtil.setEventAttribute(control, "onchange", "InfoJet_OnControlChange(event,this);");
            DOMUtil.setEventAttribute(control, "onfocus", "InfoJet_OnFocus(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onmouseover", "InfoJet_OnMouseOver(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnLinkedPictureClick(event,this,'control');");
        }

        private static void addInlineImageEvent(XmlElement control)
        {
            DOMUtil.setEventAttribute(control, "onchange", "InfoJet_OnControlChange(event,this);");
            DOMUtil.setEventAttribute(control, "onfocus", "InfoJet_OnFocus(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onmouseover", "InfoJet_OnMouseOver(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnInlinePictureClick(event,this,'control');");
        }

        //CID 0499 //2010.03.17 //Begin
        private static void addInkPictureEvent(XmlElement control)
        {
            DOMUtil.setEventAttribute(control, "onchange", "InfoJet_OnControlChange(event,this);");
            DOMUtil.setEventAttribute(control, "onfocus", "InfoJet_OnFocus(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onmouseover", "InfoJet_OnMouseOver(event,this,'control');");
            DOMUtil.setEventAttribute(control, "onclick", "InfoJet_OnInkPictureClick(event,this,'control');");
        }
        //CID 0499 //2010.03.17 //End

        //CID 0801 //2011.02.21 //xctname, enableJsonXml
        private static void assignControlID(View view, XmlElement control, string xdBinding, string xctname, bool enableJsonXml)
        {
            string binding = control.GetAttribute(xdBinding);
            if (binding.Length > 0)
            {
                //在File Attachment Formatting时也调用了.
                //CID 0714 //2010.11.29 //Begin
                string bindingValue = DOMUtil.getControlBindingExpr(view, binding);
                //CID 0714 //2010.11.29 //End
                control.SetAttribute(JetConstant.HTML_id, bindingValue);
                //CID 0773 //2011.02.10 //Begin
                setJsonXml(view, control, binding, xctname, enableJsonXml);
                //CID 0773 //2011.02.10 //End
            }
        }

        //CID 0773 //2011.02.10 //Begin
        //CID 0801 //2011.02.21 //xctname, enableJsonXml
        private static void setJsonXml(View view, XmlElement control, string binding, string xctname, bool enableJsonXml)
        {
            //CID 0801 //2011.02.21 //xctname && enableJsonXml
            if (enableJsonXml && xctname.Length > 0 && ConfigUtil.EnableControlJsonXml())
            {
                string filter = control.GetAttribute("InfoJet_ControlJsonXmlFilter");
                control.RemoveAttribute("InfoJet_ControlJsonXmlFilter");
                string jsonXmlValue = DOMUtil.getControlJsonXmlExpr(view, binding, filter);
                control.SetAttribute("JsonXml", jsonXmlValue);
            }
        }
        //CID 0787 //2011.02.10 //End

        private static bool isButtonTypeInput(XmlElement control)
        {
            string tag = control.Name.ToLower();
            if (tag.Equals("input"))
            {
                string type = control.GetAttribute("type");
                if (type != null && (type.Equals("checkbox") || type.Equals("radio")))
                {
                    return true;
                }
            }
            return false;
        }

        private static bool isTextTypeInput(XmlElement control)
        {
            string tag = control.Name.ToLower();
            if (tag.Equals("input"))
            {
                string type = control.GetAttribute("type");
                if (type != null && (type.Equals("text")))
                {
                    return true;
                }
            }
            return false;
        }

        //CID 1423 //2012.10.31 //Begin
        private static void formatFileAttachments(View view, List<XmlElement> controls, string xdBinding)
        {
            if (controls != null)
            {
                foreach (XmlElement control in controls)
                {
                    //2008.05.11 //reattachementsnextissue // Begin
                    checkNonNilllableBASE64(view, control, xdBinding);
                    //2008.05.11 //reattachementsnextissue // End
                    formatFileAttachment(view, control, xdBinding);
                }
            }
        }
        //CID 1423 //2012.10.31 //End

        //格式化附件控件.
        private static void formatFileAttachment(View view, XmlElement control, string xdBinding)
        {
            XmlDocument doc = view.Doc;

            string binding = control.GetAttribute(xdBinding);
            //CID 0861 //2011.04.12 //Begin
            if (ConfigUtil.EnableControlPSVI())
            {
                control.SetAttribute("psvi_id", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetPSVI(") + binding + ")}");
            }
            //CID 0861 //2011.04.12 //End

            //CID 0714 //2010.11.29 //Begin
            string bindingExpr = DOMUtil.getControlBindingExpr(view, binding);
            //CID 0714 //2010.11.29 //End

            XmlElement ifElement = (XmlElement)DOMUtil.selectOneNode(control, NamespaceUtil.toFullName(view.XslPrefix, "if"), view.NsResolver);
            if (ifElement != null)
            {
                control.RemoveChild(ifElement);
            }

            //CID 0495 //2010.02.05 //Begin
            control.SetAttribute("allowedFileTypes", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetAllowedFileTypes(") + binding + ")}");
            //CID 0495 //2010.02.05 //End

            //CID 1423 //2012.11.01 //Begin
            if (ConfigUtil.EnablePlaceholderUI())
            {
                control = DOMUtil.renameElementName(control, "a");
                string style = control.GetAttribute("style");
                style = style + ";cursor: default;display: inline-block;border: 1px solid gainsboro;box-sizing: border-box;table-layout: auto;padding:0px;margin:0px;";
                control.SetAttribute("style", style);
                control.SetAttribute("onclick", "javascript:InfoJet_AttachFile(this);return false;");
                control.SetAttribute("id", bindingExpr);

                XmlElement parentElement = control.ParentNode as XmlElement;
                XmlElement newControl = control.CloneNode(true) as XmlElement;
                XmlElement notExistIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                notExistIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='0'");
                XmlElement existIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                existIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='1'");
                parentElement.InsertBefore(notExistIfElement, control);
                parentElement.InsertBefore(existIfElement, control);
                parentElement.RemoveChild(control);

                notExistIfElement.AppendChild(newControl);
                existIfElement.AppendChild(control);

                //not existing file
                newControl.SetAttribute("href", "javascript:void(0)");

                XmlElement tableNewElement = doc.CreateElement("table");
                tableNewElement.SetAttribute("style", "font-family:Tahoma;font-size:8pt;color:black;;display:inline-block;margin:0px;padding:0px;border:0 none;");
                tableNewElement.SetAttribute("cellspacing", "0px");
                tableNewElement.SetAttribute("cellpadding", "0px");
                XmlElement tbodyNewElement = doc.CreateElement("tbody");
                XmlElement trNewElement = doc.CreateElement("tr");
                trNewElement.SetAttribute("style", "display: table-row;");
                newControl.AppendChild(tableNewElement);
                tableNewElement.AppendChild(tbodyNewElement);
                tableNewElement.AppendChild(trNewElement);

                XmlElement td1NewElement = doc.CreateElement("td");
                td1NewElement.SetAttribute("style", "display: table-cell;padding: 5px;");
                XmlElement div1NewElement = doc.CreateElement("div");
                XmlElement span1NewElement = doc.CreateElement("span");
                span1NewElement.SetAttribute("style", "width: 7px;display: inline-block;height: 10px;overflow: hidden;direction: ltr;");
                //width: 11px;display: inline-block;height: 30px;overflow: hidden;direction: ltr;
                XmlElement img1NewElement = doc.CreateElement("img");
                img1NewElement.SetAttribute("src", view.Manifest.ImageUri.FileSmallPlaceholder);

                trNewElement.AppendChild(td1NewElement);
                td1NewElement.AppendChild(div1NewElement);
                div1NewElement.AppendChild(span1NewElement);
                span1NewElement.AppendChild(img1NewElement);

                XmlElement td2NewElement = doc.CreateElement("td");
                td2NewElement.SetAttribute("style", "display: table-cell;padding: 5px;");
                XmlElement span2NewElement = doc.CreateElement("span");
                span2NewElement.SetAttribute("style", "color: black;margin: 0px;padding: 0px;border: 0 none;");
                span2NewElement.InnerText = "Click here to attach a file";
                trNewElement.AppendChild(td2NewElement);
                td2NewElement.AppendChild(span2NewElement);

                //existing file
                XmlElement srcAttrElement = doc.CreateElement(view.XslPrefix, "attribute", JetConstant.URI_XSL);
                srcAttrElement.SetAttribute("name", "href");
                XmlElement valueofAttrElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                valueofAttrElement.SetAttribute("select", "xdImage:getImageUrl(" + binding + ")");
                srcAttrElement.AppendChild(valueofAttrElement);
                control.AppendChild(srcAttrElement);

                XmlElement tableElement = doc.CreateElement("table");
                tableElement.SetAttribute("style", "font-family:Tahoma;font-size:8pt;color:black;;display:inline-block;margin:0px;padding:0px;border:0 none;");
                tableElement.SetAttribute("cellspacing", "0px");
                tableElement.SetAttribute("cellpadding", "0px");
                XmlElement tbodyElement = doc.CreateElement("tbody");
                XmlElement trElement = doc.CreateElement("tr");
                trElement.SetAttribute("style", "display: table-row;");
                control.AppendChild(tableElement);
                tableElement.AppendChild(tbodyElement);
                tableElement.AppendChild(trElement);

                XmlElement td1Element = doc.CreateElement("td");
                td1Element.SetAttribute("style", "display: table-cell;padding: 5px;");
                XmlElement div1Element = doc.CreateElement("div");
                XmlElement span1Element = doc.CreateElement("span");
                span1Element.SetAttribute("style", "width: 11px;display: inline-block;height: 30px;overflow: hidden;direction: ltr;");
                XmlElement img1Element = doc.CreateElement("img");
                img1Element.SetAttribute("src", view.Manifest.ImageUri.FileBigPlaceholder);

                trElement.AppendChild(td1Element);
                td1Element.AppendChild(div1Element);
                div1Element.AppendChild(span1Element);
                span1Element.AppendChild(img1Element);

                XmlElement td2Element = doc.CreateElement("td");
                td2Element.SetAttribute("style", "display: table-cell;padding: 5px;");
                XmlElement span2Element = doc.CreateElement("span");
                span2Element.SetAttribute("style", "color: black;margin: 0px;padding: 0px;border: 0 none;");
                trElement.AppendChild(td2Element);
                td2Element.AppendChild(span2Element);

                XmlElement nameElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                nameElement.SetAttribute("select", "xdImage:getFileName(" + binding + ")");
                XmlElement brElement = doc.CreateElement("br");
                XmlElement sizeElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                sizeElement.SetAttribute("select", "xdImage:getFileSize(" + binding + ")");
                span2Element.AppendChild(nameElement);
                span2Element.AppendChild(brElement);
                span2Element.AppendChild(sizeElement);

                //CID 1426 //2012.11.02 //Begin
                control.SetAttribute("isFileAttachment", "true");
                tableElement.SetAttribute("isFileAttachment", "true");
                tbodyElement.SetAttribute("isFileAttachment", "true");
                trElement.SetAttribute("isFileAttachment", "true");
                td1Element.SetAttribute("isFileAttachment", "true");
                div1Element.SetAttribute("isFileAttachment", "true");
                td2Element.SetAttribute("isFileAttachment", "true");
                span2Element.SetAttribute("isFileAttachment", "true");
                //CID 1426 //2012.11.02 //End
                //CID 1426 //2012.11.02 //Begin
                newControl.SetAttribute("isFileAttachment", "true");
                tableNewElement.SetAttribute("isFileAttachment", "true");
                tbodyNewElement.SetAttribute("isFileAttachment", "true");
                trNewElement.SetAttribute("isFileAttachment", "true");
                td1NewElement.SetAttribute("isFileAttachment", "true");
                div1NewElement.SetAttribute("isFileAttachment", "true");
                td2NewElement.SetAttribute("isFileAttachment", "true");
                span2NewElement.SetAttribute("isFileAttachment", "true");
                //CID 1426 //2012.11.02 //End
            }
            else
            {
                //如果XML中没有文件,则输出一个空的链接.
                XmlElement selectElement = doc.CreateElement("a");
                //CID 0459 //2011.04.11 //Begin
                selectElement.SetAttribute("style", "text-decoration:none;height:100%;border:solid 1px #c0c0c0;");
                //CID 0459 //2011.04.11 //End
                selectElement.SetAttribute("class", "xdFileAttachmentLink");
                selectElement.SetAttribute("href", "javascript:void(0)");
                //当点击附件链接时,直接弹出文件上传对话框.
                selectElement.SetAttribute("onclick", "javascript:InfoJet_AttachFile(this);return false;");
                //CID 0459 //2011.04.11 //Begin
                selectElement.InnerText = Convert.ToChar(0xA0) + ConfigUtil.SelectFileMessage() + Convert.ToChar(0xA0);
                //CID 0459 //2011.04.11 //End
                selectElement.SetAttribute(JetConstant.XD_xctname, JetConstant.URI_XD, JetConstant.XCT_FileAttachment);
                selectElement.SetAttribute(JetConstant.XD_binding, JetConstant.URI_XD, binding);
                selectElement.SetAttribute("id", bindingExpr);
                XmlElement selectIfElement = doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                selectIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='0'");
                selectIfElement.AppendChild(selectElement);

                //如果XML中有文件,则输出包含文件信息的链接.
                XmlElement aElement = doc.CreateElement("a");
                //CID 0459 //2011.04.11 //Begin
                aElement.SetAttribute("style", "text-decoration:none;height:100%;border:solid 1px #c0c0c0;");
                //CID 0459 //2011.04.11 //End
                aElement.SetAttribute("class", "xdFileAttachmentLink");
                aElement.SetAttribute(JetConstant.XD_xctname, JetConstant.URI_XD, JetConstant.XCT_FileAttachment);
                aElement.SetAttribute(JetConstant.XD_binding, JetConstant.URI_XD, binding);
                aElement.SetAttribute("id", bindingExpr);
                aElement.SetAttribute("onclick", "javascript:InfoJet_AttachFile(this);return false;");
                XmlElement srcAElement = doc.CreateElement(view.XslPrefix, "attribute", JetConstant.URI_XSL);
                srcAElement.SetAttribute("name", "href");
                XmlElement valueofAElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                valueofAElement.SetAttribute("select", "xdImage:getImageUrl(" + binding + ")");
                srcAElement.AppendChild(valueofAElement);
                aElement.AppendChild(srcAElement);
                XmlElement nameElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                nameElement.SetAttribute("select", "xdImage:getFileName(" + binding + ")");
                XmlElement sizeElement = doc.CreateElement(view.XslPrefix, "value-of", JetConstant.URI_XSL);
                sizeElement.SetAttribute("select", "xdImage:getFileSize(" + binding + ")");
                //CID 0459 //2011.04.11 //Begin
                aElement.AppendChild(view.Doc.CreateTextNode("" + Convert.ToChar(0xA0)));
                //CID 0459 //2011.04.11 //End
                aElement.AppendChild(nameElement);
                aElement.AppendChild(sizeElement);
                //CID 0459 //2011.04.11 //Begin
                aElement.AppendChild(view.Doc.CreateTextNode("" + Convert.ToChar(0xA0)));
                //CID 0459 //2011.04.11 //End
                XmlElement selectAElement = doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                selectAElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='1'");
                selectAElement.AppendChild(aElement);

                control.AppendChild(selectIfElement);
                control.AppendChild(selectAElement);

                //CID 0021 //2008.06.18 //Begin
                string style = control.GetAttribute("style");
                if (style != null && style.Length > 0)
                {
                    //用正则表达式改进算法.
                    style = style.ToLower();
                    int widthIndex = style.IndexOf("width:");
                    if (widthIndex >= 0)
                    {
                        int semicolonIndex = style.IndexOf(";", widthIndex);
                        if (semicolonIndex > 0 && semicolonIndex < style.Length)
                        {
                            style = style.Substring(0, widthIndex - 0) + style.Substring(semicolonIndex + 1, style.Length - semicolonIndex - 1);
                        }
                    }

                    //CID 0118 //2008.09.15 //Begin
                    //删除HEIGHT: 30px; FileAttachment在InfoPath Design Mode里是不能调整大小的.
                    //CID 0459 //2011.04.11 //Begin
                    //style = style.Replace( "height: 30px", "" );
                    style = style + ";padding:5px;";
                    //CID 0459 //2011.04.13 //End
                    control.SetAttribute("style", style);
                    //CID 0118 //2008.09.15 //End
                }
                //CID 0021 //2008.06.18 //End

            }
            //CID 1423 //2012.11.01 //End
        }

        //CID 1422 //2012.10.29 //Begin
        private static void formatInlinePictures(View view, List<XmlElement> controls, string xdBinding)
        {
            if (controls != null)
            {
                foreach (XmlElement control in controls)
                {
                    formatInlinePicture(view, control, xdBinding);
                }
            }
        }
        //CID 1422 //2012.10.29 //End

        //CID 1422 //212.10.29 //Begin
        private static void formatInlinePicture(View view, XmlElement control, string xdBinding)
        {
            addInlineImageEvent(control);
            assignControlID(view, control, xdBinding, JetConstant.XCT_InlineImage, false);
            //CID 0861 //2011.04.12 //Begin
            string binding = control.GetAttribute(xdBinding);
            if (ConfigUtil.EnableControlPSVI())
            {
                control.SetAttribute("psvi_id", "{" + NamespaceUtil.toFullName(view.XdExtensionPrefix, "GetPSVI(") + binding + ")}");
            }
            //CID 0861 //2011.04.12 //End

            if (ConfigUtil.EnablePlaceholderUI())
            {
                XmlElement parentElement = control.ParentNode as XmlElement;
                XmlElement newControl = control.CloneNode(true) as XmlElement;
                XmlElement notExistIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                notExistIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='0'");
                XmlElement existIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                existIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='1'");
                parentElement.InsertBefore(notExistIfElement, control);
                parentElement.InsertBefore(existIfElement, control);
                parentElement.RemoveChild(control);

                /*
                XmlElement topBorderSpanElement = view.Doc.CreateElement("span");
                topBorderSpanElement.SetAttribute("style", "vertical-align: top;");

                notExistIfElement.AppendChild(topBorderSpanElement);
                topBorderSpanElement.AppendChild(newControl);
                */

                notExistIfElement.AppendChild(newControl);
                existIfElement.AppendChild(control);

                XmlElement hrefElement = DOMUtil.renameElementName(newControl, "a");
                hrefElement.SetAttribute("style", "cursor: default;display:inline-block; text-decoration:none; color:black;;text-align:left;border: #dcdcdc 1px solid;");
                hrefElement.SetAttribute("title", "Click here to insert a picture");

                XmlElement middleBorderSpanElement = view.Doc.CreateElement("span");
                middleBorderSpanElement.SetAttribute("style", "border: none; vertical-align: middle; cursor: default;");

                XmlElement imgElement = view.Doc.CreateElement("img");
                imgElement.SetAttribute("src", view.Manifest.ImageUri.PicturePlaceholder);
                imgElement.SetAttribute("style", "border:none; margin:5px; vertical-align: top; white-space:nowrap;");
                imgElement.SetAttribute("alt", "");

                XmlElement textSpanElement = view.Doc.CreateElement("span");
                textSpanElement.SetAttribute("style", "font-family: Tahoma; font-size: 8pt; padding: 5px; word-wrap: break-word; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; display: inline-block;");
                textSpanElement.InnerText = "Click here to insert a picture";

                /*
                hrefElement.AppendChild(middleBorderSpanElement);
                middleBorderSpanElement.AppendChild(imgElement);
                middleBorderSpanElement.AppendChild(textSpanElement);
                */
                hrefElement.AppendChild(imgElement);
                hrefElement.AppendChild(textSpanElement);
            }
        }
        //CID 1422 //212.10.29 //End

        //CID 1429 //2012.11.02 //Begin
        private static void formatLinkPictures(View view, List<XmlElement> controls, string xdBinding)
        {
            if (controls != null)
            {
                foreach (XmlElement control in controls)
                {
                    formatLinkPicture(view, control, xdBinding);
                }
            }
        }
        //CID 1429 //2012.11.02 //End

        //CID 1429 //2012.11.02 //Begin
        private static void formatLinkPicture(View view, XmlElement control, string xdBinding)
        {
            addLinkedImageEvent(control);
            assignControlID(view, control, xdBinding, JetConstant.XCT_LinkedImage, false);
            string binding = control.GetAttribute(xdBinding);

            if (ConfigUtil.EnablePlaceholderUI())
            {
                XmlElement parentElement = control.ParentNode as XmlElement;
                XmlElement newControl = control.CloneNode(true) as XmlElement;
                XmlElement notExistIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                notExistIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='0'");
                XmlElement existIfElement = view.Doc.CreateElement(view.XslPrefix, "if", JetConstant.URI_XSL);
                existIfElement.SetAttribute("test", "xdImage:existFile(" + binding + ")='1'");
                parentElement.InsertBefore(notExistIfElement, control);
                parentElement.InsertBefore(existIfElement, control);
                parentElement.RemoveChild(control);

                notExistIfElement.AppendChild(newControl);
                existIfElement.AppendChild(control);

                XmlElement hrefElement = DOMUtil.renameElementName(newControl, "a");
                hrefElement.SetAttribute("style", "cursor: default;display:inline-block; text-decoration:none; color:black;;text-align:left;border: #dcdcdc 1px solid;");
                hrefElement.SetAttribute("title", "Click here to insert a picture");

                XmlElement middleBorderSpanElement = view.Doc.CreateElement("span");
                middleBorderSpanElement.SetAttribute("style", "border: none; vertical-align: middle; cursor: default;");

                XmlElement imgElement = view.Doc.CreateElement("img");
                imgElement.SetAttribute("src", view.Manifest.ImageUri.PicturePlaceholder);
                imgElement.SetAttribute("style", "border:none; margin:5px; vertical-align: top; white-space:nowrap;");
                imgElement.SetAttribute("alt", "");

                XmlElement textSpanElement = view.Doc.CreateElement("span");
                textSpanElement.SetAttribute("style", "font-family: Tahoma; font-size: 8pt; padding: 5px; word-wrap: break-word; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; display: inline-block;");
                textSpanElement.InnerText = "Click here to insert a picture";

                hrefElement.AppendChild(imgElement);
                hrefElement.AppendChild(textSpanElement);
            }
        }
        //CID 1429 //2012.11.02 //End
    }
}